<?php

class ChecklistAdminController extends Controller
{
    public $layout='//layouts/column2';
    public $defaultAction='admin';

    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'izin_id',
		'visible' => false,
		'value' => '($data->Izin) ? $data->Izin->id : "-"',
	),array(
		'name' => 'administrasi_id',
		'visible' => false,
		'value' => '($data->Administrasi) ? $data->Administrasi->nama : "-"',
	),array(
		'name' => 'status_kelengkapan',
		'visible' => false,
		'value' => '($data->status_kelengkapan) ? $data->status_kelengkapan : "-"',
	),array(
		'name' => 'upload_dokumen',
		'visible' => false,
		'value' => '($data->upload_dokumen) ? $data->upload_dokumen : "-"',
	),            array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn'
            ),
        );
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
	'name' => 'administrasi_id',
	'value' => ($model->administrasi) ? $model->administrasi->nama : "-",
),array(
	'name' => 'status_kelengkapan',
	'value' => ($model->status_kelengkapan) ? $model->status_kelengkapan : "-",
),array(
	'name' => 'nomor',
        'visible' => ($model->administrasi_id!=3) ? true : false,
	'value' => ($model->administrasiDetail->nomor) ? $model->administrasiDetail->nomor: "-",
),array(
	'name' => 'tanggal_surat',
        'visible'=> ($model->administrasi_id!=3 && $model->administrasi_id != 4 && $model->administrasi_id != 5 && $model->administrasi_id != 6) ? true : false,
	'value' => ($model->administrasiDetail->tanggal_surat) ? $model->administrasiDetail->tanggal_surat: "-",
),array(
	'name' => 'tanggal_terdaftar',
        'visible' => ($model->administrasi_id==4) ? true : false,
	'value' => ($model->administrasiDetail->tanggal_terdaftar) ? $model->administrasiDetail->tanggal_terdaftar: "-",
),array(
	'name' => 'tanggal_ditetapkan',
        'visible' => ($model->administrasi_id==4) ? true : false,
	'value' => ($model->administrasiDetail->tanggal_ditetapkan) ? $model->administrasiDetail->tanggal_ditetapkan: "-",
),array(
	'name' => 'berlaku_sampai',
        'visible' => ($model->administrasi_id==5 && $model->administrasi_id==6) ? true : false,
	'value' => ($model->administrasiDetail->berlaku_sampai) ? $model->administrasiDetail->berlaku_sampai: "-",
),array(
	'name' => 'nama_pejabat',
        'visible' => ($model->administrasi_id==1 && $model->administrasi_id==2) ? true : false,
	'value' => ($model->administrasiDetail->nama_pejabat) ? $model->administrasiDetail->nama_pejabat: "-",
),array(
	'name' => 'instansi_pemberi',
        'visible' => ($model->administrasi_id==4 && $model->administrasi_id==5 && $model->administrasi_id==6) ? true : false,
	'value' => ($model->administrasiDetail->instansi_pemberi) ? $model->administrasiDetail->instansi_pemberi: "-",
),array(
	'name' => 'kualifikasi_perusahaan',
        'visible' => ($model->administrasi_id==4 && $model->administrasi_id==5 && $model->administrasi_id==6) ? true : false,
	'value' => ($model->administrasiDetail->kualifikasi_perusahaan) ? $model->administrasiDetail->kualifikasi_perusahaan: "-",
),array(
	'name' => 'kegiatan_usaha_pokok',
        'visible' => ($model->administrasi_id==5) ? true : false,
	'value' => ($model->administrasiDetail->kegiatan_usaha_pokok) ? $model->administrasiDetail->kegiatan_usaha_pokok: "-",
),array(
	'name' => 'kbli',
        'visible' => ($model->administrasi_id==5) ? true : false,
	'value' => ($model->administrasiDetail->kbli) ? $model->administrasiDetail->kbli: "-",
),array(
	'name' => 'klasifikasi',
        'visible' => ($model->administrasi_id==6) ? true : false,
	'value' => ($model->administrasiDetail->klasifikasi) ? $model->administrasiDetail->klasifikasi: "-",
),array(
	'name' => 'pemberi_izin',
        'visible' => ($model->administrasi_id==9) ? true : false,
	'value' => ($model->administrasiDetail->pemberi_izin) ? $model->administrasiDetail->pemberi_izin: "-",
),array(
	'name' => 'perihal',
        'visible' => ($model->administrasi_id==9) ? true : false,
	'value' => ($model->administrasiDetail->perihal) ? $model->administrasiDetail->perihal: "-",
),array(
	'name' => 'lokasi',
        'visible' => ($model->administrasi_id==9) ? true : false,
	'value' => ($model->administrasiDetail->lokasi) ? $model->administrasiDetail->lokasi: "-",
),array(
	'name' => 'luas',
        'visible' => ($model->administrasi_id==9) ? true : false,
	'value' => ($model->administrasiDetail->luas) ? $model->administrasiDetail->luas: "-",
),array(
	'name' => 'pengesahan_kemenkumham',
        'visible' => ($model->administrasi_id==9) ? true : false,
	'value' => ($model->administrasiDetail->pengesahan_kemenkumham) ? $model->administrasiDetail->pengesahan_kemenkumham: "-",
),array(
	'name' => 'upload_dokumen',
        'type'=>'raw',
	'value' => ($model->upload_dokumen) ? CHtml::link($model->upload_dokumen, array('checklistAdmin/download/'.$model->id),array('target'=>'_blank')) : "-",
));
        return $viewList;
    }
    

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionDetail($id)
    {
        $model=$this->loadModel($id);
        $data = Izin::model()->findByPk($model->izin_id);
        $this->render('detail', array(
            'model' => $model,
            'data'=>$data,
        ));
    }
    
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }
    
    public function actionDownload($id) {
        $model = $this->loadModel($id);
        $path = YiiBase::getPathOfAlias('webroot').$model->path_dokumen;
        clearstatcache();
        ob_end_clean();
        if (!empty($model->upload_dokumen) && file_exists($path.$model->upload_dokumen)) {
            $file = $path.$model->upload_dokumen;
            header('Pragma: public');
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
            header('Cache-Control: no-store, no-cache, must-revalidate');
            header('Cache-Control: pre-check=0, post-check=0, max-age=0');
            header ("Pragma: no-cache");
            header("Expires: 0");
            header('Content-Transfer-Encoding: none');
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Type: application/pdf');
            header('Content-Length: ' . filesize($file));
            header('Content-Disposition: attachment; filename="'.basename($model->upload_dokumen).'"');
            readfile($file);
            Yii::app()->end();
            return;
        } else {
            echo 'Berkas tidak tersedia';
        }
    }

    public function actionCreate()
    {
        $model=new ChecklistAdmin;
        $this->performAjaxValidation($model);
        if(isset($_POST['ChecklistAdmin']))
        {
            $model->attributes=$_POST['ChecklistAdmin'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $adminDetail = AdministrasiDetail::model()->findByAttributes(array('checklist_admin_id'=>$id));
//        $this->performAjaxValidation($model);
        if(isset($_POST['ChecklistAdmin'],$_POST['AdministrasiDetail']))
        {
            $model->attributes=$_POST['ChecklistAdmin'];
            $adminDetail->attributes=$_POST['AdministrasiDetail'];
            $izinID = $model->izin_id;
            $data = Izin::model()->with('perusahaan')->findByPk($izinID);
                $badanUsaha = $data->perusahaan->bentuk_badan_usaha;
                $nama = $data->perusahaan->nama_perusahaan;
                $perusahaanId = $data->perusahaan_id;
                $namaItem = $model->administrasi->nama;
                
                if($data->perusahaan->jenis_usaha == 'Pengolahan MB')
                    $dirName = '1 Pengolahan MB';
                if($data->perusahaan->jenis_usaha == 'Pengolahan GB')
                    $dirName = '2 Pengolahan GB';
                if($data->perusahaan->jenis_usaha == 'Pengolahan HO')
                    $dirName = '3 Pengolahan HO';
                $num = $model->administrasi->id;
                $fileName = $num.'-'.$badanUsaha.' '.$nama.'-'.$namaItem.'.pdf';
            $file = CUploadedFile::getInstance($model, 'upload_dokumen');
            if (is_object($file) && get_class($file) === 'CUploadedFile')
                $model->upload_dokumen = $file;
            if ($model->validate()) {
                $rootDir = '/uploaded_files/attachment/'.$dirName.'/'.$perusahaanId.' '.$badanUsaha.' '.$nama.'/1 Persyaratan Administratif/';
                $path = YiiBase::getPathOfAlias('webroot').$rootDir;
                if (is_object($model->upload_dokumen)) {
                    $model->upload_dokumen->saveAs($path.$fileName);
                }
                
                $model->upload_dokumen = (!empty($model->upload_dokumen)) ? $fileName : '';
                $model->status_kelengkapan = (!empty($model->upload_dokumen)) ? 'Ada' : 'Tidak';
                $model->path_dokumen = (!empty($model->upload_dokumen)) ? $rootDir : '';
                
                if($model->save())
                {
                    if($adminDetail->validate())
                    {
                        $adminDetail->save();
                        $this->redirect(array('izin/'.$izinID));
                    }
                }
            }
        }
        $this->render('update',array(
            'model'=>$model,
            'adminDetail'=>$adminDetail,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $model=new ChecklistAdmin('publik');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['ChecklistAdmin']))
            $model->attributes=$_GET['ChecklistAdmin'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new ChecklistAdmin('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['ChecklistAdmin']))
            $model->attributes=$_GET['ChecklistAdmin'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=ChecklistAdmin::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='checklist-admin-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
